package ngames.com.sushitv.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.core.AppModel;
import ngames.com.sushitv.core.AppModelImpl;
import ngames.com.sushitv.tools.LogUtil;
import ngames.com.sushitv.view.DialogAppLoading;

public class BaseFragment extends Fragment {
    public static final String TAG = "BaseFragment==";

    protected View mBaseView;
    protected Activity mActivity;
    protected AppApplication mAppApplication;
    private boolean mFirstVisible = false;
    private boolean mHasLoadedOnce = false;
    protected AppModelImpl mAppModel;
    protected DialogAppLoading mDialogLoading;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        LogUtil.d(TAG + getClass().getName(), "setUserVisibleHint() isVisibleToUser = " + isVisibleToUser);
        if (this.isVisible()) {
            // we check that the fragment is becoming visible
            if (isVisibleToUser && !mFirstVisible) {
                // async http request here
                onFirstVisible();
                mFirstVisible = true;
            }
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        LogUtil.d(TAG + getClass().getName(), "onAttach() ");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.d(TAG + getClass().getName(), "onCreate() ");
        mActivity = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LogUtil.d(TAG + getClass().getName(), "onCreateView() ");
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LogUtil.d(TAG + getClass().getName(), "onViewCreated() ");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LogUtil.d(TAG + getClass().getName(), "onActivityCreated() ");
        if(!mHasLoadedOnce) {
            mHasLoadedOnce = true;
            onFirstLoad();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtil.d(TAG + getClass().getName(), "onStart() ");
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.d(TAG + getClass().getName(), "onResume() ");
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtil.d(TAG + getClass().getName(), "onPause() ");
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtil.d(TAG + getClass().getName(), "onStop() ");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtil.d(TAG + getClass().getName(), "onDestroyView() ");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(TAG + getClass().getName(), "onDestroy() ");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LogUtil.d(TAG + getClass().getName(), "onDetach() ");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        LogUtil.d(TAG + getClass().getName(), "onHiddenChanged() hidden = " + hidden);
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        LogUtil.d(TAG + getClass().getName(), "onViewStateRestored()");
    }

    /**
     * 第一次显示，此时还未创建view，未绑定activity。
     */
    public void onFirstVisible(){

    }

    /**
     * 第一次加载，此时还已经创建view，绑定activity。可以做一些获取初始化数据的操作
     */
    public void onFirstLoad(){

    }

    protected AppApplication getAppApplication() {
        if (mActivity == null) {
            mActivity = getActivity();
        }
        if(mAppApplication == null) {
            mAppApplication = (AppApplication) mActivity.getApplication();
        }
        return mAppApplication;
    }

    public AppModel getAppModel(){
        if (mAppModel == null || mAppApplication == null) {
            mAppModel = (AppModelImpl) getAppApplication().getAppModel();
        }
        return mAppModel;
    }

    protected void showProgress(boolean show) {
        if (show) {
            if (mDialogLoading == null) {
                mDialogLoading = new DialogAppLoading(mActivity);
            }
            if (!mDialogLoading.isShowing()) {
                mDialogLoading.show();
            }
        } else {
            if (mDialogLoading != null && mDialogLoading.isShowing()) {
                mDialogLoading.cancel();
            }
        }
    }
}
